<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      姓：<input type="text" v-model="firstName" /> + 名：<input
        type="text"
        v-model="lastName"
      />
      = <span>{{ fullName }}</span><br /><br />
      <button @click="changeName">改名卡</button>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>

    <script>
      const app = new Vue({
        el: "#app",
        data: {
          firstName: "刘",
          lastName: "备",
        },
        computed: {
          // 简写，默认是get方法
          //   fullName() {
          //     return this.firstName + this.lastName;
          //   },

          // 需要修改内容时，要有set方法
          //   set中的value是修改的值
          fullName: {
            get() {
              return this.firstName + this.lastName;
            },
            set(value) {
              //   console.log(value.slice(0, 1));
              //   console.log(value.slice(1));
              this.firstName = value.slice(0, 1);
              this.lastName = value.slice(1);
            },
          },
        },
        methods: {
          changeName() {
            this.fullName = "孙小策";
          },
        },
      });
    </script>
  </body>
</html>
